import { safeInvoke } from './tauri';

interface Setting {
  key: string;
  value: string;
}

// 获取设置项
export async function getSetting(key: string): Promise<string> {
  try {
    const setting = await safeInvoke<Setting>('get_setting', { key });
    return setting?.value || '';
  } catch (error) {
    console.error('Failed to get setting:', error);
    return '';
  }
}

// 更新设置项
export async function updateSetting(key: string, value: string): Promise<void> {
  try {
    await safeInvoke('update_setting', { 
      request: { key, value } 
    });
  } catch (error) {
    console.error('Failed to update setting:', error);
  }
}

// 全局ESC键处理器
export function setupGlobalEscapeHandler(handler: () => void) {
  const handleKeyDown = (e: KeyboardEvent) => {
    if (e.key === 'Escape') {
      handler();
    }
  };

  window.addEventListener('keydown', handleKeyDown);
  
  // 返回清理函数
  return () => {
    window.removeEventListener('keydown', handleKeyDown);
  };
}